﻿using System;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.HtmlControls;

namespace Jayden.Dll.Web.Controls.Google
{
	public class GoogleMapDiv : Div
	{
		//AIzaSyDyVJk0nPKFnasFQd8uanLvb6fa4pxcpik
		public string ServerApiKey { get { return GetServerAttribute("api-key"); } set { SetServerAttribute("api-key", value); } }
		public string ServerZoom { get { return GetServerAttribute("zoom", "8"); } set { SetServerAttribute("zoom", value); } }
		public string ServerMarkerText { get { return GetServerAttribute("marker-text", string.Empty); } set { SetServerAttribute("marker-text", value); } }
		public string ServerCoords { get { return GetServerAttribute("coords"); } set { SetServerAttribute("coords", value); } }
		public GoogleMapType ServerMapType { get { return (GoogleMapType)Enum.Parse(typeof(GoogleMapType), GetServerAttribute("map-type", "hybrid"), true); } set { SetServerAttribute("map-type", value.ToString().ToLowerInvariant()); } }
		public bool ServerDisableDefaultUI { get { return GetServerAttribute("disable-default-ui", "false") == "true"; } set { SetServerAttribute("disable-default-ui", value ? "true" : null); } }
		public bool ServerDraggable { get { return GetServerAttribute("draggable", "true") != "false"; } set { SetServerAttribute("draggable", value ? null : "false"); } }
		public bool ServerZoomControl { get { return GetServerAttribute("zoom-control", "true") != "false"; } set { SetServerAttribute("zoom-control", value ? null : "false"); } }

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader)
		{
			base.Load(node, loader);
			if (Id == null)
				Id = "googleMap";
		}

		private string m_JsInitMap = string.Empty;
		public string JsInitMap { get { return m_JsInitMap; } set { m_JsInitMap = value; } }

		public override void ProcessJsRequest(HttpHandlerContext context)
		{
			string id = GetType().FullName;
			string apiKey = ServerApiKey;
			if (apiKey == null)
				apiKey = context.Configuration.GetValue("Google/Map/@api-key", null);
			if (!context.Js.Contains(id) && !string.IsNullOrWhiteSpace(apiKey))
			{
				context.Js[id].AppendOnInit(@"
	JAYDEN.sfn.loadGoogleMaps('" + apiKey + @"');
");
			}

			id = Id;
			context.Js[id].Builder.Append(@"
JAYDEN.google.maps.array.push({
	initialize: function(obj) {
		var $map = $('#" + Id + @"');
		var latLng = new google.maps.LatLng(" + ServerCoords + @");
		var mapOptions = {
			backgroundColor: '#fff',
			center: latLng,
			disableDefaultUI: " + ServerDisableDefaultUI.ToString().ToLowerInvariant() + @",
			draggable: " + ServerDraggable.ToString().ToLowerInvariant() + @",
			mapTypeId: google.maps.MapTypeId." + ServerMapType.ToString().ToUpperInvariant() + @",
			zoom: " + ServerZoom + @",
			zoomControl: " + ServerZoomControl.ToString().ToLowerInvariant() + @"
		};
		var map = obj.map = new google.maps.Map($map[0], mapOptions);
		$map.data('googleMapInstance', map);" + m_JsInitMap + @"

		var marker = new google.maps.Marker({
			position: latLng,
			map: map,
			title: '" + ServerMarkerText + @"'
		});
	}
});
");
			base.ProcessJsRequest(context);
		}
	}
}
